package com.xhtt.modules.task.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.utils.CopyUtils;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.Query;
import com.xhtt.modules.msg.entity.MsgInfo;
import com.xhtt.modules.msg.service.MsgInfoService;
import com.xhtt.modules.msg.service.MsgUserService;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.sys.service.SysUserService;
import com.xhtt.modules.task.dao.TaskPotTaskMapper;
import com.xhtt.modules.task.entity.TaskPotPlan;
import com.xhtt.modules.task.entity.TaskPotSeo;
import com.xhtt.modules.task.entity.TaskPotTask;
import com.xhtt.modules.task.entity.TaskPotTaskStep;
import com.xhtt.modules.task.model.Confirm1Model;
import com.xhtt.modules.task.model.ConnectTaskDetail;
import com.xhtt.modules.task.model.TaskOperConnectModel;
import com.xhtt.modules.task.model.TaskOperModel;
import com.xhtt.modules.task.model.msg.PlanTaskModel;
import com.xhtt.modules.task.model.pot.ArrangeModel;
import com.xhtt.modules.task.model.pot.TaskOperCCRModel;
import com.xhtt.modules.task.model.pot.TaskOperOtherModel;
import com.xhtt.modules.task.model.ship.WorkUserModel;
import com.xhtt.modules.task.schedule.TaskDetailSchedule;
import com.xhtt.modules.task.service.*;
import com.xhtt.modules.task.utils.StaticUtils;
import com.xhtt.modules.task.utils.TaskUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

/**
 * <p>
 * 罐任务-任务分配计划 服务实现类
 * </p>
 *
 * @author feipc
 * @since 2019-03-05
 */
@Service
public class TaskPotTaskServiceImpl extends ServiceImpl<TaskPotTaskMapper, TaskPotTask> implements TaskPotTaskService {
    @Autowired
    private TaskPotPlanService taskPotPlanService;
    @Autowired
    private TaskPotTaskStepService taskPotTaskStepService;
    @Autowired
    private TaskDetailSchedule taskDetailSchedule;
    @Autowired
    private TaskUtils taskUtils;
    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;
    @Autowired
    private TaskStepService taskStepService;
    @Autowired
    private MsgInfoService msgInfoService;
    @Autowired
    private MsgUserService msgUserService;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private TaskPotSeoService potSeoService;
    @Resource
    private TransactionTemplate transactionTemplate;


    @Override
    public ArrangeModel getTaskDetail(Integer planId, SysUserEntity user) {
        ArrangeModel sm = new ArrangeModel();
        TaskPotPlan potPlan = taskPotPlanService.getById(planId);
        if (null == potPlan) {
            throw new RRException("对象不存在");
        }
        sm.setPlanId(planId);
        sm.setMainTaskName(potPlan.getTaskName());
        sm.setProductId(potPlan.getProductId());
        sm.setProductName(potPlan.getProductName());
        List<TaskPotTask> taskList = this.list(new LambdaQueryWrapper<TaskPotTask>().eq(TaskPotTask::getPotPlanId, planId));
        if (potPlan.getStatus().intValue() == 2)//如果在待分配状态下，就只能看自己组的任务数据
        {
            //过滤下任务列表
            taskList.removeIf(t -> (null == t.getExecTeamId() || t.getExecTeamId().intValue() != user.getTeamId().intValue()));
        }
        //找到排管任务再处理
        for (TaskPotTask task : taskList) {
            if (null == task.getIfPhoto()) {
                task.setIfPhoto(0);//默认设置0
            }
            if (null == task.getIfConfirm()) {
                task.setIfConfirm(0);//默认设置0
            }
            if (task.getType().intValue() == 1) {
                //再找连接步骤
                List<TaskPotTaskStep> taskSteps = taskPotTaskStepService.list(new LambdaQueryWrapper<TaskPotTaskStep>().eq(TaskPotTaskStep::getPotTaskId, task.getId()).orderByAsc(TaskPotTaskStep::getStep));
                task.setTaskStepList(taskSteps);
            }

            taskDetailSchedule.getTaskDetail(task);
        }
        sm.setTasks(taskList);

        return sm;
    }

    @Override
    @Transactional
    public void pubTask(List<TaskPotTask> tasks, SysUserEntity user) {
        List<TaskPotTask> dbs = new ArrayList<>();
        TaskPotTask db;
        Integer planId = 0;
        List<Integer> thisIds = new ArrayList<>();//
        for (TaskPotTask taskPotTask : tasks) {
            thisIds.add(taskPotTask.getId());
            if (CollectionUtils.isNotEmpty(taskPotTask.getUserAssistModel())) {
                taskPotTask.setUserAssist(JSON.toJSONString(taskPotTask.getUserAssistModel()));
            } else {
                taskPotTask.setUserAssist(JSON.toJSONString(Collections.EMPTY_LIST));
            }
            if (taskPotTask.getType().intValue() == 1) {
                taskPotTask.setIfConfirm(1);//管线任务默认要确认
            }
            db = this.getById(taskPotTask.getId());
            CopyUtils.copyProperties(taskPotTask, db);
            db.setStatus(4);
            db.setPubTime(LocalDateTime.now());
            //判断下是不是有确认人，因为除了管线任务会手动设置 其他任务要默认选某组的组长（选了需要确认的话）
            if (null == db.getConfirm1User() && db.getIfConfirm().intValue() == 1) {
                db.setConfirm1User(user.getUserId().intValue());
                db.setConfirm1UserName(user.getName());

                db.setConfirm2UserName(user.getName());
                db.setConfirm2User(user.getUserId().intValue());//省得烦，直接设置成一样的
            }
            //判断有没有任务频率,也就ccr任务会有了 fuck
            if (StringUtils.isNotEmpty(db.getTaskFre())) {
                int count = StaticUtils.gettaskCount(db.getTaskFre(), db.getTaskStartDate(), db.getTaskEndDate());
                db.setCcrTotal(count);
                db.setCcrDone(0);
            }

            dbs.add(db);
            planId = taskPotTask.getPotPlanId();
        }
        this.updateBatchById(dbs);

        //然后保存软管连接节点们
        TaskPotTask hoseTask = tasks.stream().filter(taskPotTask -> 1 == taskPotTask.getType().intValue()).findFirst().orElse(null);//拿出软管连接任务
        String productName = "";
        if (null != hoseTask) {
            TaskPotPlan plan = taskPotPlanService.getOne(new LambdaQueryWrapper<TaskPotPlan>().eq(TaskPotPlan::getId, planId).select(TaskPotPlan::getProductId, TaskPotPlan::getProductName));
            productName = plan.getProductName();
            List<TaskPotTaskStep> taskSteps = hoseTask.getTaskStepList();
            Integer taskId = hoseTask.getId();
            taskExecutor.submit(() -> taskStepService.synchronizeFunc(taskSteps, taskId, plan.getProductId(), plan.getProductName(), user));
        }
        //更新主任务状态为4
        //如果这个主任务的所有子任务都被分配完了 那就更新主任务状态为4
        int count = this.count(new LambdaQueryWrapper<TaskPotTask>().eq(TaskPotTask::getPotPlanId, planId).
                and(qw -> qw.eq(TaskPotTask::getStatus, 0).or(qw2 -> qw2.eq(TaskPotTask::getStatus, 2)))
                .notIn(TaskPotTask::getId, thisIds));
        if (count == 0) {
            TaskPotPlan plan = new TaskPotPlan();
            plan.setId(planId);
            plan.setStatus(4);
            taskPotPlanService.updateById(plan);
        }
        String pdname = productName;
        //发消息
        taskExecutor.submit(() -> {
            List<MsgInfo> msgList = new ArrayList<>();
            for (TaskPotTask potTask : dbs) {
                PlanTaskModel ptm = new PlanTaskModel(potTask.getId(), potTask.getMainTaskName(), potTask.getName(), pdname, potTask.getTaskStartDate(), potTask.getUserAssistModel());
                Set<Integer> userIds = new HashSet<>();
                MsgInfo msg = new MsgInfo("计划任务", "倒罐:" + potTask.getMainTaskName(), "任务待完成提交", JSON.toJSONString(ptm));
                userIds.add(potTask.getUserMainId());
                msg.setUserIdList(userIds);
                msgList.add(msg);
            }
            msgInfoService.saveMsgBatch(msgList);
        });

    }

    @Override
    public PageUtils confirmTaskList(Map<String, Object> params) {
        Page<TaskPotTask> page = new Query<TaskPotTask>(params).getPage();
        page.setRecords(baseMapper.confirmTaskList(page, params));
        return new PageUtils(page);
    }

    @Override
    public TaskOperModel taskOperDetail(Integer taskId, Integer userId) {
        TaskPotTask task = this.getById(taskId);
        if (null == task) {
            throw new RRException("无数据");
        }
        TaskPotPlan plan = taskPotPlanService.getById(task.getPotPlanId());
        String operDetail = task.getOperDetail();
        if (StringUtils.isEmpty(operDetail)) {
            if (1 == task.getType().intValue()) {//排管任务
                //获取这个产品的连接管道 组
                List<TaskPotTaskStep> steps = taskPotTaskStepService.list(new LambdaQueryWrapper<TaskPotTaskStep>().eq(TaskPotTaskStep::getPotTaskId, taskId).orderByAsc(TaskPotTaskStep::getStep));
                //转换成model里面的steps
                List<ConnectTaskDetail> connectTaskDetails = new ArrayList<>();
                steps.forEach(p -> {
                    ConnectTaskDetail connectTaskDetail = new ConnectTaskDetail();
                    connectTaskDetail.setPointName(p.getTypeName());
                    String rfid = taskUtils.getRfid(null == p.getType() ? 0 : p.getType().intValue(), null == p.getTypeId() ? 0 : p.getTypeId().intValue());
                    connectTaskDetail.setRfid(rfid);
                    if (StringUtils.isEmpty(rfid)) {
                        connectTaskDetail.setStatus(1);//为空默认已经扫描，
                    }
                    connectTaskDetails.add(connectTaskDetail);
                });

                TaskOperConnectModel connectModel = new TaskOperConnectModel();
                connectModel.setTaskId(taskId);
                connectModel.setProductName(plan.getProductName());
                connectModel.setConnectTaskDetailList(connectTaskDetails);
                if (userId.intValue() == task.getConfirm1User().intValue()) {
                    connectModel.setWhichConfirmUser(1);
                } else if (userId.intValue() == task.getConfirm2User().intValue()) {
                    connectModel.setWhichConfirmUser(2);
                }
                connectModel.setConfirm1Model(new Confirm1Model(connectTaskDetails, null, null));//这里提前设置下确认人1的确认详情对象
                return connectModel;
            } else if (2 == task.getType().intValue()) {//CCR任务
                TaskOperCCRModel ccrModel = new TaskOperCCRModel();
                ccrModel.setTaskId(taskId);
                return ccrModel;
            } else if (3 == task.getType().intValue() || 3 < task.getType().intValue()) {//其他任务？
                TaskOperOtherModel model = new TaskOperOtherModel();
                model.setTaskId(taskId);
                SysUserEntity user = sysUserService.getOne(new LambdaQueryWrapper<SysUserEntity>().eq(SysUserEntity::getUserId, task.getUserMainId()).select(SysUserEntity::getUserId, SysUserEntity::getName));
                model.setUserMain(null == user ? "" : user.getName());
                String userAssist = task.getUserAssist();
                if (StringUtils.isNotEmpty(userAssist)) {
                    List<WorkUserModel> us = JSON.parseArray(userAssist, WorkUserModel.class);
                    List<String> ss = us.stream().map(workUserModel -> workUserModel.getName()).collect(Collectors.toList());
                    model.setUserAssist(String.join(",", ss));
                }
                return model;
            }
        } else {
            if (1 == task.getType().intValue()) {
                TaskOperConnectModel connectModel = JSON.parseObject(operDetail, TaskOperConnectModel.class);
                connectModel.setConfirm1Result(task.getConfirm1Status());
                connectModel.setConfirm1Reason(task.getConfirm1Reason());
                connectModel.setConfirmResult(task.getConfirm2Status());
                connectModel.setConfirmReason(task.getConfirm2Reason());
                if (userId.intValue() == task.getConfirm1User().intValue()) {
                    connectModel.setWhichConfirmUser(1);
                } else if (userId.intValue() == task.getConfirm2User().intValue()) {
                    connectModel.setWhichConfirmUser(2);
                }
                return connectModel;
            } else if (2 == task.getType().intValue()) {
                //这个接口app调用 获取ccr任务详情，只获取当前操作的这次ccr记录
                List<com.xhtt.modules.task.model.pot.TaskOperCCRModel> ccrModelList = JSON.parseArray(operDetail, com.xhtt.modules.task.model.pot.TaskOperCCRModel.class);
                ccrModelList.forEach(taskOperCCRModel -> {
                    taskOperCCRModel.setConfirmReason(task.getConfirm2Reason());
                    taskOperCCRModel.setConfirmResult(task.getConfirm2Status());
                });
                int done = task.getCcrDone();
                int nowTime = done + 1;//本次操作次数
                if (ccrModelList.size() == nowTime)//说明之前临时保存过了，就直接取
                {
                    return ccrModelList.get(nowTime - 1);
                } else {
                    com.xhtt.modules.task.model.pot.TaskOperCCRModel model = new com.xhtt.modules.task.model.pot.TaskOperCCRModel();
                    model.setTaskId(taskId);
                    return model;
                }
            } else if (3 == task.getType().intValue() || 3 < task.getType().intValue()) {
                TaskOperOtherModel otherModel = JSON.parseObject(operDetail, TaskOperOtherModel.class);
                otherModel.setConfirmResult(task.getConfirm2Status());
                otherModel.setConfirmReason(task.getConfirm2Reason());
                return otherModel;
            }
        }
        return null;
    }

    @Override
    public void tempSave(TaskOperModel model) {
        TaskPotTask task = this.getById(model.getTaskId());
        if (null == task) {
            throw new RRException("对象不存在");
        }
        checkTask(task, model);
//        task.setPubTime(LocalDateTime.now());为什么会设置分配时间呢？？？我也不知道了，但是说不通啊，所以就注释了
        this.updateById(task);
    }

    @Override
    public void submitTask(TaskOperModel model) {
        AtomicBoolean mainComplete = new AtomicBoolean(false);
        TaskPotTask tt = transactionTemplate.execute(transactionStatus -> {
            try {
                TaskPotTask task = this.getById(model.getTaskId());
                if (null == task) {
                    throw new RRException("对象不存在");
                }
                boolean skipConfirm = null == model.getSkipConfirm() ? false : model.getSkipConfirm();//是否直接不用干活直接提交
                if (!skipConfirm) {
                    checkTask(task, model);
                    //ccr任务要判断操的次数
                    if (task.getType().intValue() == 2)//CCR
                    {
                        //设置本次的完成时间-取出最后一条更新一下时间就好了
                        List<TaskOperCCRModel> ccrList = JSON.parseArray(task.getOperDetail(), TaskOperCCRModel.class);
                        int size = ccrList.size();
                        TaskOperCCRModel lastCCR = ccrList.get(size - 1);
                        lastCCR.setFinishTime(LocalDateTime.now());
                        task.setOperDetail(JSON.toJSONString(ccrList));

                        int done = task.getCcrDone() + 1;
                        int total = task.getCcrTotal();
                        task.setCcrDone(done);
                        if (total != done)//说明这次ccr没做完
                        {
                            this.updateById(task);
                            return null;
                        }
                    }
                }
                task.setFinishTime(LocalDateTime.now());//完成时间设置
                task.setStatus(9);//状态设置
                //判断是否要确认，不要的话直接把确认状态改成1了,状态改成10 已确认
                if (task.getIfConfirm() == 0) {
                    task.setConfirm1Status(1);
                    task.setConfirm2Status(1);
                    task.setStatus(10);
                }
                this.updateById(task);
                //判断是否所有子任务都做完了，是的话就更新主任务的状态为9
                int count = this.count(new LambdaQueryWrapper<TaskPotTask>().eq(TaskPotTask::getPotPlanId, task.getPotPlanId()).ne(TaskPotTask::getId, task.getId()).lt(TaskPotTask::getStatus, 9));
                if (count == 0) {
                    TaskPotPlan p = new TaskPotPlan();
                    p.setStatus(9);
                    p.setId(task.getPotPlanId());
                    //如果自己是不用确认的，那就判断是否所有子任务都确认完了，是的话就更新主任务的状态为10
                    if (task.getIfConfirm() == 0) {
                        int countC = this.count(new LambdaQueryWrapper<TaskPotTask>().eq(TaskPotTask::getPotPlanId, task.getPotPlanId()).ne(TaskPotTask::getId, task.getId()).lt(TaskPotTask::getStatus, 10));
                        if (countC == 0) {
                            p.setStatus(10);
                            mainComplete.set(true);
                        }
                    }
                    taskPotPlanService.updateById(p);
                }
                return task;
            } catch (Throwable e) {
                transactionStatus.setRollbackOnly();
                return null;
            }
        });

        if (null != tt) {
            //发消息
            taskExecutor.submit(() -> {
                if (tt.getIfConfirm() == 1) {
                    TaskPotPlan plan = taskPotPlanService.getOne(new LambdaQueryWrapper<TaskPotPlan>().eq(TaskPotPlan::getId, tt.getPotPlanId()).select(TaskPotPlan::getTaskName, TaskPotPlan::getProductName));
                    PlanTaskModel ptm = new PlanTaskModel(tt.getId(), tt.getMainTaskName(), tt.getName(), plan.getProductName(), tt.getTaskStartDate(), tt.getUserAssistModel());
                    MsgInfo msg = new MsgInfo("计划任务", "倒罐:" + plan.getTaskName(), "任务待确认", JSON.toJSONString(ptm));

                    //一个人确认？2个人确认？
                    List<Integer> userIds = new ArrayList<>();
                    if (tt.getType().intValue() == 1)//管线任务 2人确认
                    {
                        userIds.add(tt.getConfirm1User());
                    } else {
                        userIds.add(tt.getConfirm2User());
                    }
                    msgUserService.insertBatch(msg, userIds);
                }
            });
            if (mainComplete.get()) {
                //任务完成要同步数据到seo
                taskExecutor.submit(() -> {
                    TaskPotSeo seo = taskPotPlanService.dataToSeo(tt.getPotPlanId());
                    potSeoService.save(seo);
                });
            }
        }
    }

    @Override
    public void commitTask(TaskOperModel model, Integer userId) {
        AtomicBoolean mainComplete = new AtomicBoolean(false);
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                taskPotPlanService.getById(model.getTaskId());
                status.setRollbackOnly();
            }
        });
        TaskPotTask tt = transactionTemplate.execute(transactionStatus -> {
            try {
                TaskPotTask task = this.getById(model.getTaskId());
                if (task.getIfConfirm().intValue() == 1) {
                    if (task.getConfirm1User().equals(userId) && null == task.getConfirm1Status()) {
                        task.setConfirm1Status(model.getConfirmResult());
                        task.setConfirm1Reason(model.getConfirmReason());
                    }
                    if (task.getConfirm2User().equals(userId) && null == task.getConfirm2Status()) {
                        task.setConfirm2Status(model.getConfirmResult());
                        task.setConfirm2Reason(model.getConfirmReason());
                        task.setStatus(10);//确认完成
                        //判断是不是子任务都确认完了( 除了自己以外，没有状态小于9的数据了)，是的话把主任务状态也改一下
                        Integer potPlanId = task.getPotPlanId();
                        int count = this.count(new LambdaQueryWrapper<TaskPotTask>().eq(TaskPotTask::getPotPlanId, potPlanId).lt(TaskPotTask::getStatus, 10).ne(TaskPotTask::getId, task.getId()));
                        if (0 == count) {
                            TaskPotPlan plan = new TaskPotPlan();
                            plan.setId(potPlanId);
                            plan.setStatus(10);
                            taskPotPlanService.updateById(plan);
                            mainComplete.set(true);
                        }
                    }

                    if (task.getType().intValue() == 1 && null == task.getConfirm2Status())//如果是管线连接任务，并且发现第二个人还没确认 ，那就给第二个人发消息，
                    {
                        //保存确认详情
                        TaskOperConnectModel cm = JSON.parseObject(task.getOperDetail(), TaskOperConnectModel.class);
                        CopyUtils.copyProperties(model, cm);
                        task.setOperDetail(JSON.toJSONString(cm));
                    }

                }
                this.updateById(task);
                return task;
            } catch (Throwable e) {
                transactionStatus.setRollbackOnly();
                return null;
            }
        });

        if (null != tt && mainComplete.get()) {
            //任务完成要同步数据到seo
            taskExecutor.submit(() -> {
                TaskPotSeo seo = taskPotPlanService.dataToSeo(tt.getPotPlanId());
                potSeoService.save(seo);
            });
        }
    }

    @Override
    public List<TaskPotTask> shiftsList(Integer selfId) {
        return baseMapper.shiftsList(selfId);
    }

    @Override
    public List<TaskOperCCRModel> taskCCRDetail(Integer taskId) {
        TaskPotTask task = this.getOne(new LambdaQueryWrapper<TaskPotTask>().eq(TaskPotTask::getId, taskId).select(TaskPotTask::getId, TaskPotTask::getOperDetail));
        if (null == task) {
            throw new RRException("对象不存在");
        }
        String detail = task.getOperDetail();
        if (StringUtils.isEmpty(detail)) {
            return Collections.EMPTY_LIST;
        } else {
            List<TaskOperCCRModel> list = JSON.parseArray(detail, TaskOperCCRModel.class);
            return list;
        }
    }

    @Override
    public String taskCCRStatus(Integer taskId) {
        TaskPotTask task = this.getOne(new LambdaQueryWrapper<TaskPotTask>().eq(TaskPotTask::getId, taskId).select(TaskPotTask::getId, TaskPotTask::getCcrDone, TaskPotTask::getOperDetail));
        if (null == task) {
            throw new RRException("对象不存在");
        }
        int done = task.getCcrDone();
        String detail = task.getOperDetail();
        if (StringUtils.isEmpty(detail)) {
            return "未开始";
        }
        int nowTime = done + 1;//本次的次数
        List<TaskOperCCRModel> modelList = JSON.parseArray(detail, TaskOperCCRModel.class);
        if (modelList.size() < nowTime) {
            return "未开始";
        } else {
            return "进行中";
        }
    }

    @Override
    public void start(Integer taskId) {
        TaskPotTask task = this.getOne(new LambdaQueryWrapper<TaskPotTask>().eq(TaskPotTask::getId, taskId).select(TaskPotTask::getId, TaskPotTask::getType, TaskPotTask::getOperDetail, TaskPotTask::getStartTime));
        if (null == task) {
            throw new RRException("对象不存在");
        }
        if (task.getType().intValue() == 2)//如果是ccr任务 他妈的还要特殊处理，真的是日了🐶了
        {
            TaskOperCCRModel ccr = new TaskOperCCRModel();
            ccr.setStartTime(LocalDateTime.now());
            String detail = task.getOperDetail();
            List<TaskOperCCRModel> ccrList;
            if (StringUtils.isNotEmpty(detail)) {
                ccrList = JSON.parseArray(detail, com.xhtt.modules.task.model.pot.TaskOperCCRModel.class);
                ccrList.add(ccr);
            } else {
                ccrList = new ArrayList<>();
                ccrList.add(ccr);
            }
            task.setOperDetail(JSON.toJSONString(ccrList));
        }
        task.setStartTime(LocalDateTime.now());
        this.updateById(task);
    }

    private void checkTask(TaskPotTask task, TaskOperModel model) {
        //判断下类型吧
        int type = task.getType().intValue();
        if (1 == type) {
            TaskOperConnectModel connectModel;
            try {
                connectModel = (TaskOperConnectModel) model;
            } catch (Exception e) {
                throw new RRException("类型与所传参数不匹配");
            }
            task.setOperDetail(JSON.toJSONString(connectModel));
        } else if (2 == type) {
            TaskOperCCRModel ccrModel;
            try {
                ccrModel = (TaskOperCCRModel) model;
            } catch (Exception e) {
                throw new RRException("类型与所传参数不匹配");
            }
            //ccr任务跟狗屎一样恶心的需求处理
            String detail = task.getOperDetail();
            if (StringUtils.isEmpty(detail))//第一次添加
            {
                List<com.xhtt.modules.task.model.pot.TaskOperCCRModel> list = new ArrayList<>();
                list.add(ccrModel);
                task.setOperDetail(JSON.toJSONString(list));
            } else {
                int done = task.getCcrDone();
                int nowTime = done + 1;//本次操作的次数
                List<com.xhtt.modules.task.model.pot.TaskOperCCRModel> list = JSON.parseArray(detail, com.xhtt.modules.task.model.pot.TaskOperCCRModel.class);
                if (list.size() == nowTime)//说明临时保存过
                {
                    //那就把这条更新一下把
                    list.set(nowTime - 1, ccrModel);
                } else//说明当前是第一次保存这次，有点绕口，反正就是这个意思
                {
                    //那就把这次的对象放进list
                    list.add(ccrModel);
                }
                task.setOperDetail(JSON.toJSONString(list));
            }
        } else if (3 <= type) {
            try {
                TaskOperOtherModel otherModel = (TaskOperOtherModel) model;
                otherModel.setUserMain(sysUserService.getOne(new LambdaQueryWrapper<SysUserEntity>().eq(SysUserEntity::getUserId, task.getUserMainId())).getName());
                String userAssist = task.getUserAssist();
                if (StringUtils.isNotEmpty(userAssist)) {
                    List<WorkUserModel> us = JSON.parseArray(userAssist, WorkUserModel.class);
                    List<String> ss = us.stream().map(workUserModel -> workUserModel.getName()).collect(Collectors.toList());
                    otherModel.setUserAssist(String.join(",", ss));
                }
                task.setOperDetail(JSON.toJSONString(otherModel));
            } catch (Exception e) {
                throw new RRException("类型与所传参数不匹配");
            }
        } else {
            throw new RRException("无效的类型");
        }
    }

}
